//
//  _4DudelViewController.m
//  04Dudel
//
//  Created by smu on 11-7-27.
//  Copyright 2011年 duk. All rights reserved.
//

#import "_4DudelViewController.h"
#import "DudelView.h"
#import "PencilTool.h"
#import "LineTool.h"
#import "RectangleTool.h"
#import "EllipseTool.h"
#import "FreehandTool.h"

@implementation _4DudelViewController

@synthesize currentTool, fillColor, strokeColor, strokeWidth;

- (void)deselectAllToolButtons{
    [freehandButton setImage:[UIImage imageNamed:@"button_bezier.png"]];
	[ellipseButton setImage:[UIImage imageNamed:@"button_ellipse.png"]];
	[rectangleButton setImage:[UIImage imageNamed:@"button_rectangle.png"]];
	[lineButton setImage:[UIImage imageNamed:@"button_line.png"]];
	[pencilButton setImage:[UIImage imageNamed:@"button_cdots.png"]];
}

- (void)setCurrentTool:(id <Tool>)t
{
    [currentTool deactivate];
    if (t != currentTool) {
        [currentTool release];
        currentTool = [t retain];
        currentTool.delegate = self;
        [self deselectAllToolButtons];
    }
    [currentTool activate];
    [dudelView setNeedsDisplay];
}

-(IBAction)touchFreehandItem:(id)sender
{
    self.currentTool = [FreehandTool sharedFreehandTool];
	[freehandButton setImage:[UIImage imageNamed:@"button_bezier_selected.png"]];
}

-(IBAction)touchEllipseItem:(id)sender
{
    self.currentTool = [EllipseTool sharedEllipseTool];
    [ellipseButton setImage:[UIImage imageNamed:@"button_ellipse_selected.png"]];
}

-(IBAction)touchRectangleItem:(id)sender
{
    self.currentTool = [RectangleTool sharedRectangleTool];
    [rectangleButton setImage:[UIImage imageNamed:@"button_rectangle_selected.png"]];
}

-(IBAction)touchLineItem:(id)sender
{
    self.currentTool = [LineTool sharedLineTool];
    [lineButton setImage:[UIImage imageNamed:@"button_line_selected.png"]];
}

-(IBAction)touchPencilItem:(id)sender
{
    self.currentTool = [PencilTool sharedPencilTool];
    [pencilButton setImage:[UIImage imageNamed:@"button_cdots_selected.png"]];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [currentTool touchesBegan:touches withEvent:event];
    [dudelView setNeedsDisplay];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [currentTool touchesCancelled:touches withEvent:event];
    [dudelView setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [currentTool touchesEnded:touches withEvent:event];
    [dudelView setNeedsDisplay];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [currentTool touchesMoved:touches withEvent:event];
    [dudelView setNeedsDisplay];
}


- (void)dealloc
{
    self.currentTool = nil;
    self.fillColor = nil;
    self.strokeColor = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.currentTool = [PencilTool sharedPencilTool];
    [pencilButton setImage:[UIImage imageNamed:@"button_cdots_selected.png"]];
    self.fillColor = [UIColor lightGrayColor];
    self.strokeColor = [UIColor blackColor];
    self.strokeWidth = 2.0;
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

- (IBAction)touchSendPdfEmailItem:(id)sender
{
    //set up PDF rendering context
    NSMutableData *pdfData = [NSMutableData data];
    UIGraphicsBeginPDFContextToData(pdfData, dudelView.bounds, nil); //创建一个PDF图形上下文，将pdf内容存储在pdfData中
//    UIGraphicsBeginPDFContextToFile pdf 存储在文件中
    UIGraphicsBeginPDFPage();//创建一个pdf页面,才可以将内容绘制到定义的区域，使用默认页面大小新建页面
    //定制页面的大小和各种属性 UIGraphicsBeginPDFPageWithInfo
    // tell our view to draw
    [dudelView drawRect:dudelView.bounds];
    
    // remove PDF rendering context
    UIGraphicsPopContext();
    
    // send PDF data in mail message
    MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController alloc] init] autorelease];
    mailComposer.mailComposeDelegate = self;
    [mailComposer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"Dudel creation.pdf"];
    [self presentModalViewController:mailComposer animated:YES];
}


#pragma mark MFMailComposeViewControllerDelegate

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    [self dismissModalViewControllerAnimated:YES];
}

#pragma mark ToolDelegate
- (void)addDrawable:(id<Drawable>)d
{
    [dudelView.drawables addObject:d];
    [dudelView setNeedsDisplay];
}

- (UIView *)viewForUseWithTool:(id<Tool>)t
{
    return self.view;
}

#pragma  mark DudelViewDelegate
- (void)drawTemporary
{
    [self.currentTool drawTemporary];
}

@end
